package de.sendorian.app.forumArchive.domain;


import static de.sendorian.util.StringUtils.*;
import static org.apache.commons.lang.StringUtils.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
public class Post {

	@Id
	@GeneratedValue
	private int id;

	@ManyToOne
	private User author;

	@Column(length = 4000)
	private String message;

	private Date postDate;

	@OneToMany(cascade = CascadeType.ALL)
	@Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
	@JoinTable(name = "POST_IMAGES")
	@Fetch(FetchMode.SUBSELECT)
	private List<Image> images = new ArrayList<Image>();

	@OneToMany(cascade = CascadeType.ALL)
	@Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
	@JoinTable(name = "POST_ARCHIVES")
	@Fetch(FetchMode.SUBSELECT)
	private List<Archive> archives = new ArrayList<Archive>();

	private ArrayList<String> externalLinks = new ArrayList<String>();


	public int getId() {
		return id;
	}


	public User getAuthor() {
		return author;
	}


	public void setAuthor(User author) {
		this.author = author;
	}


	public String getMessage() {
		return message;
	}


	public void setMessage(String message) {
		this.message = trim(removeEmptyLines(message));
	}


	public Date getPostDate() {
		return postDate;
	}


	public void setPostDate(Date postDate) {
		this.postDate = postDate;
	}


	public List<Image> getImages() {
		return images;
	}


	public void setImages(List<Image> images) {
		this.images = images;
	}


	 public List<Archive> getArchives() {
		return archives;
	}


	public void setArchives(List<Archive> archives) {
		this.archives = archives;
	}


	public List<String> getExternalLinks() {
		return externalLinks;
	}


	public void setExternalLinks(List<String> externalLinks) {
		this.externalLinks = new ArrayList<String>();
		this.externalLinks.addAll(externalLinks);
	}


	public String toString() {
		String postDate = this.postDate != null ? " on "
		    + SimpleDateFormat.getDateTimeInstance().format(this.postDate) : "";
		int imageCount = images != null ? images.size() : 0;
		int archiveCount = archives != null ? archives.size() : 0;
		int externalLinkcount = externalLinks != null ? externalLinks.size() : 0;
		return "Post by " + author + postDate + " with " + imageCount + " images, " + archiveCount
		    + " archives and " + externalLinkcount + " external links: "
		    + abbreviate(remove(message, "\n"), 80);
	}


	public String toStringFormatted() {
		int imageCount = images != null ? images.size() : 0;
		int archiveCount = archives != null ? archives.size() : 0;

		StringBuilder builder = new StringBuilder();
		builder.append(LINE);
		builder.append(boxCentered("Post by " + getAuthor() + " on "
		    + SimpleDateFormat.getDateTimeInstance().format(postDate)));
		builder.append(LINE);
		builder.append(boxMultiline(removeEmptyLines(getMessage())));
		builder.append(LINE);
		if (imageCount > 0 || archiveCount > 0) {
			builder.append(boxCentered("With " + imageCount + " images and " + archiveCount
			    + " archives."));
			if (imageCount > 0) {
				for (File image : images) {
					builder.append(box(" * " + image.getName()));
				}
			}
		} else {
			builder.append(boxCentered("Has no files."));
		}
		builder.append(LINE);
		builder.append("\n");
		return builder.toString();
	}

	private final static String LINE = box(StringUtils.repeat("_", TEXT_WIDTH));
}
